package cn.xie.domain.strategy.service.raffle;

import cn.xie.domain.strategy.model.entity.RuleActionEntity;
import cn.xie.domain.strategy.model.entity.RuleFillerEntity;
import cn.xie.domain.strategy.model.vo.RuleFillerCheckVO;
import cn.xie.domain.strategy.repository.IStrategyRepository;
import cn.xie.domain.strategy.service.armory.IStrategyDispatch;
import cn.xie.domain.strategy.service.rule.IRuleFiller;
import cn.xie.domain.strategy.service.rule.factory.DefaultRuleFactory;
import cn.xie.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: xiaoxie
 * create: 2025-08-09 00:28
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.domain.strategy.service.raffle
 * description: 默认的抽奖策略实现
 */
@Service
@Slf4j
public class DefaultRaffleStrategy extends AbstractRaffleStrategy {

    @Resource
    private DefaultRuleFactory ruleFactory;
    public DefaultRaffleStrategy(IStrategyRepository repository, IStrategyDispatch strategyDispatch) {
        super(repository, strategyDispatch);
    }

    @Override
    protected RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> doStrategyRuleFiller(RuleFillerEntity ruleFillerEntity, String... ruleModel) {
        // 先获取到规则工厂的map
        Map<String, IRuleFiller<RuleActionEntity.RaffleRuleBeforeEntity>> stringIRuleFillerMap = ruleFactory.openLogicFilter();
        // 优先过滤黑名单
        String blackListModel = Arrays.stream(ruleModel).filter(model -> model.contains(Constants.StrategyRule.RULE_BLACK_LIST))
                .findFirst()
                .orElse(null);
        if(StringUtils.hasText(blackListModel)) {
            IRuleFiller<RuleActionEntity.RaffleRuleBeforeEntity> raffleRuleEntityIRuleFiller = stringIRuleFillerMap.get(blackListModel);
            RuleFillerEntity rfEntity = new RuleFillerEntity();
            rfEntity.setUserId(ruleFillerEntity.getUserId());
            rfEntity.setStrategyId(ruleFillerEntity.getStrategyId());
            rfEntity.setRuleModel(Constants.StrategyRule.RULE_BLACK_LIST);
            RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> filler = raffleRuleEntityIRuleFiller.filler(rfEntity);
            if(!RuleFillerCheckVO.ALLOW.getCode().equals(filler.getCode())) {
                return filler;
            }
        }
        List<String> ruleModelList = Arrays.stream(ruleModel).filter(rule -> !rule.equals(Constants.StrategyRule.RULE_BLACK_LIST))
                .collect(Collectors.toList());
        RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> ruleActionEntity = null;
        // 在执行其他策略
        for(String rule : ruleModelList) {
            IRuleFiller<RuleActionEntity.RaffleRuleBeforeEntity> raffleRuleEntityIRuleFiller = stringIRuleFillerMap.get(rule);
            RuleFillerEntity rfEntity = new RuleFillerEntity();
            rfEntity.setUserId(ruleFillerEntity.getUserId());
            rfEntity.setStrategyId(ruleFillerEntity.getStrategyId());
            rfEntity.setRuleModel(rule);
            ruleActionEntity = raffleRuleEntityIRuleFiller.filler(rfEntity);
            log.info("抽奖前规则过滤 userId: {} ruleModel: {} code: {} info: {}",ruleFillerEntity.getUserId(), ruleModel, ruleActionEntity.getCode(), ruleActionEntity.getInfo());
            if(!RuleFillerCheckVO.ALLOW.getCode().equals(ruleActionEntity.getCode())) {
                return ruleActionEntity;
            }
        }
        return ruleActionEntity;
    }
}
